package crypt;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;

/**
 * Consts and utilities for the project
 */
public class EncryptUtil
{

    /* --- key Info --- */

    /** The size of the symetric key */
    public static final int ENCRYPTION_KEY_SIZE = 128;

	/* --- Algorithms --- */

    /** The algorithm for the secure random */
    public static final String SECURE_RANDOM_ALGORITHM = "SHA1PRNG";

    /** The algorithm to create symentric keys */
    public static final String KEY_GENERATOR_ALGORITHM = "AES";

    /** The algorithm to encrtpy and decrypt */
    public static final String DATA_CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";

    /** The algorithm to encrtpy and decrypt */
    public static final String KEY_ENCRYPTION_ALGORITHM = "RSA";

    /** The algorithm to use to sign the plain data */
    public static final String SIGNATURE_ALGORITHM = "SHA1withRSA";

	/* --- KeyStore consts --- */

    /** The password for the keystore */
    //public static final String PASSWORDS = "111221";

    /** The path to the keystore file in the HD */
    public static final String KEYSTORE_PATH = ".keystore";

    /** The alias for the cipher key entry in the key store */
    public static final String KEY_ALIAS = "alias";

    /** The alias for the RSA private key the cert entries in the key store */
    public static final String RSA_ALIAS = "rsa";

	/**
	 * Read the KeyStore from the file system
	 * 
	 * @return The created/read KeyStore
	 */
	public static KeyStore getKeyStore(String password) throws FileNotFoundException,
	KeyStoreException, IOException, NoSuchAlgorithmException,
	CertificateException {
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(KEYSTORE_PATH);
			KeyStore keyStore = KeyStore.getInstance("JCEKS");
			keyStore.load(fis, password.toCharArray());
			return keyStore;
		} finally {
			if(fis != null){
				fis.close();
			}
		}
	}
	
	/**
	 * Read all the bytes of the argument file  path
	 * @param path The path to the file to read
	 * @return the read data
	 * @throws IOException on any read problem
	 */
	public static byte[] readFully(String path) throws IOException{
		File readFrom = new File(path);
		byte[] data = new byte[(int)readFrom.length()];
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(readFrom);
			fis.read(data);
			return data;
		} finally {
			if(fis != null) {
				fis.close();
			}
		}
	}
	
	/**
	 * Convert a byte array into its hex dump representation
	 * @param array The byte[] to hex dump
	 * @return String
	 */
	public static String getHexDump(byte[] array){
		final char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
	    char[] hexChars = new char[array.length * 2];
	    int v;
	    for ( int j = 0; j < array.length; j++ ) {
	        v = array[j] & 0xFF;
	        hexChars[j * 2] = hexArray[v >>> 4];
	        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
	    }
	    return new String(hexChars);
	}
	
}
